package com.ruoyi.aitdemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.aitcommon.service.MapService;
import com.ruoyi.aitcommon.vo.AggMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.service.SQLService;

/**
 * service示例
 * 可以新增flow service方法
 * 
 * @author Administrator
 *
 */
@Service
public class AITDemoService {
	@Autowired
	SQLService sqlService;
	@Autowired
	MapService  mapService;

	/**
	 * 新增服务
	 * @param msg
	 */
	@ServiceLog("输出示例")
	public void print(String msg) {
		System.out.println(msg);
	}
	
	/**
	 * 新增服务
	 * @param sql
	 * @param db
	 */
	@ServiceLog("SQL示例")  
	public List<Map<String,Object>> sqlQuery(String sql,JSONObject db) throws Exception{
		return sqlService.execQuery(sql, db); 
	}

	/**
	 * 单据组合代码示例
	 * @param datas SQL查询数据集合
	 * @param params 转换参数,举例: {"headKey":"docNo","fenge":"fengge","toBodyName":"lines"}
	 * @return 单据聚合结果集合
	 */
	@ServiceLog("单据组合示例")
	public List<Map<String,Object>> transAggVO(List<Map<String,Object>> datas,Map<String,Object> params) throws Exception{
		List<Map<String,Object>> retlist=new ArrayList<>();
		//将数据转换为聚合对象,含单头单体
		final List<AggMap> aggMaps = mapService.transAggVO(datas, params.get("headKey").toString(), params.get("fenge").toString());
		for(AggMap aggMap : aggMaps){
			//将AggVO转换为指定结构
			retlist.add(aggMap.toMap(null, params.get("toBodyName").toString()));
		}
		return retlist;
	}
}
